package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.*;

/**
 * Canal监听器 ---> 监听数据变化, 一旦数据发生变化, 将会触发其中的方法
 */
@CanalEventListener
public class DataListener {

   /* @ListenPoint(schema = "leadnews_article", table = "ap_article")
    public void listenDataChange(CanalEntry.EventType eventType , CanalEntry.RowData rowData){
        System.out.println("------------> ap_article表结构数据发生变化, 变化的类型: " + eventType);

        rowData.getBeforeColumnsList().stream().forEach(column -> {
            System.out.println("Before 操作之前数据: "+column.getName()+" : "+column.getValue());
        });

        System.out.println("======================================================");

        rowData.getAfterColumnsList().stream().forEach(column -> {
            System.out.println("Before 操作之后数据: "+column.getName()+" : "+column.getValue());
        });
    }*/

    @InsertListenPoint(schema = "leadnews_article", table = "ap_article")
    public void listenDataChangeInsert(CanalEntry.EventType eventType , CanalEntry.RowData rowData){
        System.out.println("------------> ap_article表结构数据发生 : INSERT");
        rowData.getAfterColumnsList().stream().forEach(column -> {
            System.out.println("Before 操作之后数据: "+column.getName()+" : "+column.getValue());
        });
    }



    @UpdateListenPoint(schema = "leadnews_article", table = "ap_article")
    public void listenDataChangeUpdate(CanalEntry.EventType eventType , CanalEntry.RowData rowData){
        System.out.println("------------> ap_article表结构数据发生 : UPDATE");

        rowData.getBeforeColumnsList().stream().forEach(column -> {
            System.out.println("Before 操作之前数据: "+column.getName()+" : "+column.getValue());
        });

        System.out.println("======================================================");

        rowData.getAfterColumnsList().stream().forEach(column -> {
            System.out.println("Before 操作之后数据: "+column.getName()+" : "+column.getValue());
        });
    }


    @DeleteListenPoint(schema = "leadnews_article", table = "ap_article")
    public void listenDataChangeDelete(CanalEntry.EventType eventType , CanalEntry.RowData rowData){
        System.out.println("------------> ap_article表结构数据发生 : DELETE");

        rowData.getBeforeColumnsList().stream().forEach(column -> {
            System.out.println("Before 操作之前数据: "+column.getName()+" : "+column.getValue());
        });
    }

}
